<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
/**
 * Script file of HelloWorld component
 */
class com_jmonoslideshowInstallerScript
{
	/**
	 * method to install the component
	 *
	 * @return void
	 */
	function install($parent) 
	{
		
		
		$db = & JFactory::getDBO();
		$query = "UPDATE #__modules SET position='icon', ordering=1, published=1 WHERE module='mod_jmonoslideshow_quickicon'";
		$db->setQuery($query);
		$db->query();
		
		echo '<div style="background-color:#e5f4bb; border-radius: 10px; padding: 5px 5px 5px 30px; color:#000; margin:10px;"><img src="../media/com_jmonoslideshow/images/admin/accept.png" align="absmiddle" /> '.JText::_( "COM_JMONOSLIDEHOW_INSTALL_OK" ).'	</div>';
		
		$this->installExtension($parent);

	}
 
	/**
	 * method to uninstall the component
	 *
	 * @return void
	 */
	 
	
	 
	function uninstall($parent) 
	{
		echo '<p>' . JText::_('COM_JMONOSLIDESHOW_UNINSTALL_TEXT') . '</p>';
	}
 
	/**
	 * method to update the component
	 *
	 * @return void
	 */
	function update($parent) 
	{
		$msgSQL = "";
		
		$updateAuthor 	= false;
		$errorMsg	= '';
		$updateAuthor = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "author", "INT( 11 ) NOT NULL DEFAULT '0'", "description" );
		if (!$updateAuthor) {
			$msgSQL .= 'Error while updating AUTHOR column';
		}
		
		$updateCreate 	= false;
		$errorMsg	= '';
		$updateCreate = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "created", "datetime NOT NULL default '0000-00-00 00:00:00'", "author" );
		if (!$updateCreate) {
			$msgSQL .= 'Error while updating CREATED column';
		}
		
		$updateLink 	= false;
		$errorMsg	= '';
		$updateLink = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "link", "varchar(255) NOT NULL default ''", "description" );
		if (!$updateLink) {
			$msgSQL .= 'Error while updating LINK column';
		}
		
		$updateModifier 	= false;
		$errorMsg	= '';
		$updateModifier = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "modified", "datetime NOT NULL default '0000-00-00 00:00:00'", "created" );
		if (!$updateModifier) {
			$msgSQL .= 'Error while updating modified column';
		}
		
		$updateModifiedBy 	= false;
		$errorMsg	= '';
		$updateModifiedBy = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "modified_by", "INT( 11 ) NOT NULL DEFAULT '0'", "modified" );
		if (!$updateModifiedBy) {
			$msgSQL .= 'Error while updating modified_by column';
		}
		
		$updateType = false;
		$errorMsg	= '';
		$updateType = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "type", "tinyint( 1 ) NOT NULL DEFAULT '0'", "link" );
		if (!$updateType) {
			$msgSQL .= 'Error while updating type column';
		}
		
		$updateParams = false;
		$errorMsg	= '';
		$updateParams = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "params", "text NOT NULL", "ordering" );
		if (!$updateParams) {
			$msgSQL .= 'Error while updating params column';
		}
		
		$updateExtension = false;
		$errorMsg	= '';
		$updateExtension = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "extension", "varchar(4) NOT NULL default ''", "type" );
		if (!$updateExtension) {
			$msgSQL .= 'Error while updating extension column';
		}
		
		$updateAlbumid = false;
		$errorMsg	= '';
		$updateAlbumid = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "albumid", "int(11) NOT NULL default '0'", "catid" );
		if (!$updateAlbumid) {
			$msgSQL .= 'Error while updating albumid column';
		}
		
		$updateAssetid = false;
		$errorMsg	= '';
		$updateAssetid = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetid) {
			$msgSQL .= 'Error while updating asset_id column';
		}

		$updateAssetidModel = false;
		$errorMsg	= '';
		$updateAssetidModel = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_model", "asset_id", "int(11) NOT NULL default '0'", "id" );
		if (!$updateAssetidModel) {
			$msgSQL .= 'Error while updating asset_id column for category table';
		}
		
		$updateAccessMedia = false;
		$errorMsg	= '';
		$updateAccessMedia = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessMedia) {
			$msgSQL .= 'Error while updating asset_id column for media table';
		}
		
		$updateAccessModel = false;
		$errorMsg	= '';
		$updateAccessModel = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow_model", "access", "INTEGER UNSIGNED	NOT NULL DEFAULT '0'", "params" );
		if (!$updateAccessModel) {
			$msgSQL .= 'Error while updating asset_id column for model table';
		}
		
		$updateFilenameMedia = false;
		$errorMsg	= '';
		$updateFilenameMedia = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "filename", "varchar(255) NOT NULL DEFAULT ''", "albumid" );
		if (!$updateFilenameMedia) {
			$msgSQL .= 'Error while updating filename column for media table';
		}
		
		$updateLinkRssMedia = false;
		$errorMsg	= '';
		$updateLinkRssMedia = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "link_rss", "varchar(255) NOT NULL DEFAULT ''", "params" );
		if (!$updateLinkRssMedia) {
			$msgSQL .= 'Error while updating Link Rss column for media table';
		}
		
		$updateRssMedia = false;
		$errorMsg	= '';
		$updateRssMedia = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "rss", "tinyint(1) NOT NULL DEFAULT '0'" );
		if (!$updateRssMedia) {
			$msgSQL .= 'Error while updating Rss column for media table';
		}
		
		$updateThumbName = false;
		$errorMsg	= '';
		$updateThumbName = $this->AddColumnIfNotExists( $errorMsg, "#__monoslideshow", "thumbname", "varchar(255) NOT NULL DEFAULT ''", 'filename' );
		if (!$updateThumbName) {
			$msgSQL .= 'Error while updating Thubname column for media table';
		}
		
		if (!$msgSQL) {
				echo '<div style="background-color:#e5f4bb; border-radius: 10px; padding: 5px 5px 5px 30px; color:#000; margin:10px;"><img src="../media/com_jmonoslideshow/images/admin/accept.png" align="absmiddle" /> '.JText::_( "COM_JMONOSLIDEHOW_UPGRADE_OK" ).'	</div>';
			} 
			else 
			{
				echo '<div style="background-color:#f8d0ca; border-radius: 10px; padding: 5px 5px 5px 30px; color:#000; margin:10px;"><img src="../media/com_jmonoslideshow/images/admin/cancel.png" align="absmiddle" /> '.JText::_( "COM_JMONOSLIDEHOW_UPGRADE_ERROR" ).' : '.$msgSQL.'	</div>'; 
			}
		
		$this->installExtension($parent);
	}
	
	function AddColumnIfNotExists(&$errorMsg, $table, $column, $attributes = "INT( 11 ) NOT NULL DEFAULT '0'", $after = '' ) {
		
		global $mainframe;
		$db				=& JFactory::getDBO();
		$columnExists 	= false;

		$query = 'SHOW COLUMNS FROM '.$table;
		$db->setQuery( $query );
		if (!$result = $db->query()){return false;}
		$columnData = $db->loadObjectList();
		
		foreach ($columnData as $valueColumn) {
			if ($valueColumn->Field == $column) {
				$columnExists = true;
				break;
			}
		}
		
		if (!$columnExists) {
			if ($after != '') {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.' AFTER '.$db->nameQuote($after).';';
			} else {
				$query = 'ALTER TABLE '.$db->nameQuote($table).' ADD '.$db->nameQuote($column).' '.$attributes.';';
			}
			$db->setQuery( $query );
			if (!$result = $db->query()){return false;}
			$errorMsg = 'notexistcreated';
		}
		
		return true;
	}
 
	/**
	 * method to run before an install/update/uninstall method
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{

		echo '<h1><img src="../media/com_jmonoslideshow/images/admin/icon-48-jmonoslideshow.png" align="absmiddle" /> '.JText::_( "COM_JMONOSLIDEHOW_INSTALL" ).'	</h1>';
			
		if (version_compare(PHP_VERSION, '5.0.0', '<')) 
			{
				echo '<div class="fc-error">';
				echo JText::_( 'COM_JMONOSLIDEHOW_UPGRADE_PHP' ) . '<br />';
				echo '</div>';
				return false;
			}
	}
	
	function installExtension($parent)
	{
		
		//Source => http://itprism.com/blog/multiple-extensions-single-package
		$manifest = $parent->get("manifest");
		$parent = $parent->getParent();
		$source = $parent->getPath("source");
		
		$installer = new JInstaller();
		
		$html = "";
		
		// Install plugins
		/*foreach($manifest->plugins->plugin as $plugin) {
		$attributes = $plugin->attributes();
		$plg = $source . DS . $attributes['folder'].DS.$attributes['plugin'];
		$installer->install($plg);
		}*/
		
		$html .= '<table class="adminlist"><thead><tr><th class="title">'.JText::_('COM_JMONOSLIDEHOW_INSTALL_MODULES').'</th><th width="60%">'.JText::_('COM_JMONOSLIDEHOW_INSTALL_STATUS').'</th></tr></thead><tfoot><tr><td colspan="2">&nbsp;</td></tr></tfoot><tbody>';
		
		// Install modules
		foreach($manifest->modules->module as $module) {
			$attributes = $module->attributes();
			$mod = $source . DS . $attributes['folder'].DS.$attributes['modules'];
			$result = $installer->install($mod);
			$style = $result ? 'font-weight: bold; color: green;' : 'font-weight: bold; color: red;';
			$resultText = $result ? JText::_('COM_JMONOSLIDEHOW_INSTALL_INSTALLED_SUCCESSFULLY') : JText::_('COM_JMONOSLIDEHOW_INSTALL_NOT_INSTALLED');
			$html .= '<tr class="row"><td class="key">'.JText::_($attributes['name']).'</td><td><span style="'.$style.'">'.$resultText.'</span></td></tr>';
		}
        $html .= '</tbody></table>';
		echo $html;
		

	}
}?>